#include "../logsys/log.hpp"
// using namespace XKL;

void test()
{
    XKL::Logger::ptr logger = XKL::LoggerManager::GetInstance().GetLogger("sync-logger");
    // int count = 0;
    // logger->debug(__FILE__, __LINE__, "%s-%d", "日志测试", count++);
    // logger->info(__FILE__, __LINE__, "%s-%d", "日志测试", count++);
    // logger->warn(__FILE__, __LINE__, "%s-%d", "日志测试", count++);
    // logger->error(__FILE__, __LINE__, "%s-%d", "日志测试", count++);
    // logger->fatal(__FILE__, __LINE__, "%s-%d", "日志测试", count++);

    // while (count < 100005)
    // {
    //     logger->fatal(__FILE__, __LINE__, "%s-%d", "日志测试", count++);
    // }
    
    logger->debug("日志测试");
    logger->info("%s", "日志测试");
    logger->warn("%s", "日志测试");
    logger->error("%s", "日志测试");
    logger->fatal("%s", "日志测试");
}

int main()
{
    // std::string filename = "./abc/bcd/aaa.txt";
    // Util::File::CreateDirectory(Util::File::Path(filename));
    // std::cout << LogLevel::ToString(LogLevel::DEBG) << std::endl;
    // std::cout << LogLevel::ToString(LogLevel::INFO) << std::endl;
    // std::cout << LogLevel::ToString(LogLevel::WARN) << std::endl;
    // std::cout << LogLevel::ToString(LogLevel::ERROR) << std::endl;
    // std::cout << LogLevel::ToString(LogLevel::FATAL) << std::endl;
    // std::cout << LogLevel::ToString(LogLevel::UNKNOW) << std::endl;
    // std::cout << LogLevel::ToString(LogLevel::OFF) << std::endl;

    // LogMsg msg(LogLevel::DEBG, 53, "main.cc", "主日志器", "格式化控制调试...");
    // Fromatter fmt;
    // std::string str = fmt.format(msg);
    // std::cout << str << std::endl;
    // std::string filename = "./logfile/test.log";
    // LogSink::ptr stdout_lsp = SinkFactory::Create<StdoutSink>();
    // LogSink::ptr file_lsp = SinkFactory::Create<FileSink>(filename);
    // LogSink::ptr roll_lsp = SinkFactory::Create<RollSink>("./logfile/test-", 1024 * 1024);
    
    // stdout_lsp->Log(str.c_str(), str.size());
    // file_lsp->Log(str.c_str(), str.size());
    // size_t curr_size = 0;
    // while (curr_size < 1024 * 1024 * 10)
    // {
    //     roll_lsp->Log(str.c_str(), str.size());
    //     curr_size += str.size();
    // }

    std::string logger_name = "sync-logger";
    XKL::LogLevel::Value limit = XKL::LogLevel::Value::DEBUG;
    XKL::Formatter::ptr formatter(new XKL::Formatter);
    std::string filename = "./logfile/test.log";
    XKL::LogSink::ptr stdout_lsp = XKL::SinkFactory::Create<XKL::StdoutSink>();
    XKL::LogSink::ptr file_lsp = XKL::SinkFactory::Create<XKL::FileSink>(filename);
    // LogSink::ptr roll_lsp = SinkFactory::Create<RollSink>("./logfile/test-", 1024 * 1024);
    // std::vector<LogSink::ptr> sinks = { stdout_lsp, file_lsp, roll_lsp };
    // Logger::ptr logger(new SyncLogger(limit, formatter, logger_name, sinks));

    std::unique_ptr<XKL::LoggerBuilder> builder(new XKL::GlobalLoggerBuilder());
    builder->BuildLoggerLevel(limit);
    // builder->EnableUnsafeAsync();
    builder->BuildLoggerName("sync-logger");
    builder->BuildLoggerType(XKL::LoggerType::LOGGER_SYNC);
    builder->BuildFormater("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
    builder->BuildSink<XKL::StdoutSink>();
    builder->BuildSink<XKL::FileSink>(filename);
    builder->Build();
    // builder->BuildSink<RollSink>("./logfile/test-", 1024 * 1024);
    
    test();
    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if(!ifs.is_open())
    // {
    //     std::cout << "open file error" << std::endl;
    //     abort();
    // }
    // // 值的认真总结
    // ifs.seekg(0, std::ios::end); // 读写位置跳转到文件结尾
    // size_t fsize = ifs.tellg();  // 获取当前读写位置相当于起始的偏移量
    // ifs.seekg(0, std::ios::beg); // 读写位置跳转到文件起始
    // std::string body;
    // body.resize(fsize);

    // ifs.read(&body[0], fsize);
    // ifs.close();

    // Buffer buff;
    // for(int i = 0; i < fsize; i++)
    // {
    //     buff.push(&body[i], 1);
    // }
    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // int cfsize = buff.ReadAbleSize();
    // for(int i = 0; i < cfsize; i++)
    // {
    //     ofs.write(buff.begin(), 1);
    //     buff.MoveReader(1);
    // }
    // ofs.close();

    return 0;
}